package com.atzongmaotan.datastructures;

/**
 * ClassName:    ArrayQueue
 * Package:  com.atzongmaotan.datastructures
 * Description:
 *
 * @Author: interface
 * @Create: 2024/9/29 - 下午7:01
 * @Version: v1.0
 */
public class ArrayQueue<T> {
    private Object[] queue;
    private int front;
    private int rear;
    private int maxSize;
    public ArrayQueue(int capacity) {
        maxSize = capacity;
        front = -1;
        rear = -1;
        queue = new Object[capacity];
    }
    public boolean isFull(){
        return rear == maxSize - 1;
    }
    public boolean isEmpty(){
        return rear == front;
    }
    public void enqueue(T element){
        if(isFull()){
            System.out.println("Queue is full");
            return ;
        }
        queue[++rear] = element;
    }
    public T dequeue(){
        if(isEmpty()){
            throw new RuntimeException("Queue is empty");
        }
        return (T) queue[++front];
    }
    // 显示队列的所有数据
    public void showQueue() {
        // 遍历
        if (isEmpty()) {
            System.out.println("queue is empty");
            return;
        }
        for (int i = 0; i < queue.length; i++) {
            System.out.printf("arr[%d]=%s\n", i, queue[i]);
        }
    }
    // 显示队列的头数据， 注意不是取出数据
    public T headQueue() {
        // 判断
        if (isEmpty()) {
            throw new RuntimeException("队列空的，没有数据~~");
        }
        return (T) queue[front + 1];
    }
    public void getFront(){
        System.out.println("front is " + front);
        System.out.println("rear is " + rear);
    }

    public static void main(String[] args) {
        ArrayQueue<String> queue = new ArrayQueue<String>(3);
        queue.enqueue("a");
        queue.enqueue("b");
        queue.enqueue("c");
        queue.enqueue("d");
        System.out.println(queue.dequeue());
        queue.getFront();
        System.out.println(queue.headQueue());
        queue.showQueue();
    }
}
